package com.panfeng.xcloud.boss.provider.member.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.panfeng.xcloud.boss.provider.member.dto.request.SecondaryVerifyReqDTO;
import com.panfeng.xcloud.boss.provider.member.dto.request.StartCaptchaReqDTO;
import com.panfeng.xcloud.boss.provider.member.dto.response.SecondaryVerifyRespDTO;
import com.panfeng.xcloud.boss.provider.member.dto.response.StartCaptchaRespDTO;
import com.panfeng.xcloud.boss.provider.member.service.IGeetestService;
import com.panfeng.xcloud.boss.provider.member.web.vo.request.SecondaryVerifyReqVO;
import com.panfeng.xcloud.boss.provider.member.web.vo.request.StartCaptchaReqVO;
import com.panfeng.xcloud.boss.provider.member.web.vo.response.SecondaryVerifyRespVO;
import com.panfeng.xcloud.boss.provider.member.web.vo.response.StartCaptchaRespVO;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.common.core.web.vo.ResponseVO;
import com.panfeng.xcloud.common.security.annotation.MethodValidate;
import com.panfeng.xcloud.common.security.annotation.NotNeedAccess;
import com.panfeng.xcloud.common.security.web.support.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * 极验行为验证服务模块
 *
 * @author xiaobo
 * @version 1.0
 * @since 2019-08-08
 */
@ApiIgnore
@RestController
@RequestMapping("user/gt")
@Api(tags = {"极验行为验证服务模块"})
@Slf4j
public class UserGtController extends BaseController {

    @Autowired
    private IGeetestService iGeetestService;

    /**
     * 极验行为验证--初始化
     *
     * @param startCaptchaReqVO
     * @param bindingResult
     * @return
     */
    @MethodValidate
    @NotNeedAccess
    @PostMapping(value = "/initialize", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "极验行为验证--初始化(肖博)", httpMethod = "POST", notes = "极验行为验证--初始化(肖博)")
    public ResponseVO<StartCaptchaRespVO> initialize(@RequestBody @Valid StartCaptchaReqVO startCaptchaReqVO, BindingResult bindingResult) {
        logger.info(">>>> 当前initialize请求参数：{} <<<", (null != startCaptchaReqVO ? JSONObject.toJSONString(startCaptchaReqVO) : ""));
        StartCaptchaReqDTO startCaptchaReqDTO = ConvertUtils.convert(startCaptchaReqVO, StartCaptchaReqDTO.class);
        StartCaptchaRespDTO startCaptchaRespDTO = iGeetestService.initialize(startCaptchaReqDTO);
        StartCaptchaRespVO startCaptchaRespVO = ConvertUtils.convert(startCaptchaRespDTO, StartCaptchaRespVO.class);
        logger.info(">>>> 当前initialize响应内容：{} <<<", (null != startCaptchaRespVO ? JSONObject.toJSONString(startCaptchaRespVO) : ""));
        return ResponseVO.with(startCaptchaRespVO);
    }

    /**
     * 极验行为验证--二次验证
     *
     * @param secondaryVerifyReqVO
     * @param bindingResult
     * @return
     */
    @MethodValidate
    @NotNeedAccess
    @PostMapping(value = "/secondaryVerify", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "极验行为验证--二次验证(肖博)", httpMethod = "POST", notes = "极验行为验证--二次验证(肖博)")
    public ResponseVO<SecondaryVerifyRespVO> secondaryVerify(@RequestBody @Valid SecondaryVerifyReqVO secondaryVerifyReqVO, BindingResult bindingResult) {
        logger.info(">>>> 当前secondaryVerify请求参数：{} <<<", (null != secondaryVerifyReqVO ? JSONObject.toJSONString(secondaryVerifyReqVO) : ""));
        SecondaryVerifyReqDTO secondaryVerifyReqDTO = ConvertUtils.convert(secondaryVerifyReqVO, SecondaryVerifyReqDTO.class);
        SecondaryVerifyRespDTO secondaryVerifyRespDTO = iGeetestService.secondaryVerify(secondaryVerifyReqDTO);
        SecondaryVerifyRespVO secondaryVerifyRespVO = ConvertUtils.convert(secondaryVerifyRespDTO, SecondaryVerifyRespVO.class);
        logger.info(">>>> 当前secondaryVerify响应内容：{} <<<", (null != secondaryVerifyRespVO ? JSONObject.toJSONString(secondaryVerifyRespVO) : ""));
        return ResponseVO.with(secondaryVerifyRespVO);
    }

}
